<template>
    <div class="authority-info-box">
        <!---->
        <div v-transfer-dom>
            <confirm v-model="phoneNumModel"
                     :close-on-confirm="false"
                     :show-cancel-button="false"
                     :title="'请输入你的身份证号'"
                     @on-confirm="confirmBtn">
                <x-input style="border:1px solid #999;"  :min="18" :max="18" type="text"
                         v-model.trim="cardId"></x-input>
            </confirm>
        </div>
    </div>

</template>

<script>

    import {cookie, Confirm, TransferDomDirective as TransferDom, XInput} from 'vux';
    import systemApi from '../../api/api';
    import store from '../../store/index';
    import statusCodeManage from '../../api/statusCodeManage';

    export default {
        name: 'authority',
        data() {
            return {
                phoneNumModel: false,
                cardId: null,
                openIdCode: null,
            };
        },
        directives: {
            TransferDom
        },
        components: {
            cookie,
            Confirm,
            XInput
        },
        methods: {
            confirmBtn() {
                let _this = this;
                systemApi.bindUser({openId: _this.openIdCode, cardId: _this.cardId}).then(
                    res => {
                        if (res.code == 0) {
                            _this.phoneNumModel = false;
                            _this.cardId = null;
                            cookie.set('userId', res.data.id);

                            let tempType =  sessionStorage.getItem("indexType");
                            if(tempType == "" || tempType == undefined){
                                tempType = this.$route.query.type;
                            }
                            _this.gotoPageByType(tempType);
                        } else {
                            _this.$vux.toast.text(res.msg, 'middle');
                        }
                    },
                    error => {
                        _this.$vux.toast.text(res.msg, 'middle');
                    }
                )
            },
            goBackPage() {
                this.$router.back();
            },
            gotoPageByType(tempType){
                console.log(tempType);
                let _this = this;
                let gotoUrl = location.origin+location.pathname+"#/";
                tempType = Number(tempType);
                switch (tempType){
                    case 15 : location.href = gotoUrl+"index/circularList"+"?type="+tempType;break;  // 重大问题通报
                    case 16 : location.href = gotoUrl+'index/circularList'+"?type="+tempType;break;  // 专项问题通报
                    case 17 : location.href = gotoUrl+'index/circularList'+"?type="+tempType;break;  // 重大项目进度通报
                    case 18 : location.href = gotoUrl+'index/circularList'+"?type="+tempType;break;  // 政策性通报
                    case 13 : location.href = gotoUrl+'index/noticeList'+"?type="+tempType;break;  // 通知

                    case 21 : location.href = gotoUrl+'index/shareList'+"?type="+tempType;break;  // 方案技术分享
                    case 22 : location.href = gotoUrl+'index/shareList'+"?type="+tempType;break;  // 培训课件分享
                    case 25 : location.href = gotoUrl+'index/uploadingList'+"?type="+tempType;break;  // 政策文件
                    case 27 : location.href = gotoUrl+'index/videoList'+"?type="+tempType;break;  // 视频

                    case 31 : location.href = gotoUrl+'index/questionList'+"?type="+tempType;break;  // 重庆 - 售后问题
                    case 41 : location.href = gotoUrl+'index/odoracleList'+"?type="+tempType;break;  // 成都-报表查询

                    case 52 : location.href = gotoUrl+'index/bookList'+"?type="+tempType;break;  // 通讯录

                    default:location.href = gotoUrl+'index/noticeList'+"?type="+tempType;break;  // 默认跳转通知
                }
            },
            initPage() {
//          先从 cookie 中获取 userid
                let _userId = cookie.get('userId');
                let _tempUrl = location.href;
                let _this = this;

                if (_tempUrl.indexOf("authzhonglian") > -1) {
                    // 从腾讯回来
                    let _codeId = location.href.split("=")[1].split("&")[0];
//                   ajax请求 http://cd.zlsoft.com/api/wx/openId?code=123 code授权成功拿到的code ，
// 此时服务器会通过code去微信服务器请求openid,拿到openid后再从数据库换取userId，
// 然后返回结果给前端，前端根据返回结果，判断是进入正常流程还是进入用户注册界面
                    systemApi.getUserId({code: _codeId}).then(
                        res => {
                            if (res.code == 0) {
                                // 返回用户信息 id ,openid
                                // {"code":0,"data":{"addTime":1528469172000,"deptId":1,"id":2,"modifyTime":1530109869000,"name":"张三2","openId":"11","phone":"18683797587"},"msg":"成功"}
                                cookie.set('userId', res.data.id);
                                let _type = sessionStorage.getItem("indexType");
                                _this.gotoPageByType(_type);
                            } else if (res.code == 1) {
                                //  系统级问题
                                _this.$vux.toast.text(res.msg, 'middle');
                                console.log(res);
                            } else if (res.code == 2) {
                                // 当前openid未绑定用户信息
//                                请输入你的 凭证
                                store.commit('UPDATE_LOADING', {isLoading: false});
                                _this.openIdCode = res.msg;
                                _this.phoneNumModel = true;
                                console.log(res);

                            }
                        },
                        error => {
                            console.log(error);
                        }
                    )
               }else if(_userId == '' || _userId == undefined ){
                      location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf3ae4842f7d5e58a&redirect_uri=http://cd.zlsoft.com/&response_type=code&scope=snsapi_base&state=authzhonglian#wechat_redirect"
               }else{
                   let tempType =  sessionStorage.getItem("indexType");
                   if(tempType == "" || tempType ==undefined || tempType == null|| tempType!="undefined"){
                       tempType = this.$route.query.type;
                   }
                    this.gotoPageByType(tempType);

               }
            }
        },
        activated() {
            store.commit('UPDATE_LOADING', {isLoading: false});
            let _type = this.$route.query.type;
            if(_type!= "" && _type!=undefined && _type!=null && _type!="undefined"){
                sessionStorage.setItem("indexType",_type);
            }
            this.initPage()
        }
    };
</script>

<style lang="less">
    @import "../../style/theme.less";
    @import "../../style/mixin.less";

    .authority-info-box {
        background-color: @color-background-default;

        .vux-header {
            .color-linear-gradient(@color-primary-light, @color-primary, 90deg);
            .vux-header-left {
                .left-arrow:before {
                    border: solid 1px @color-white;
                    border-width: 2px 0 0 2px;
                }
            }
        }
        .progressState div p {
            font-size: @font-size-tiny;
        }
        .weui-wepay-flow__state {
            width: 16px;
            height: 16px;
            top: -1px;
            padding-top: 1px;
        }
        .currentStep .weui-wepay-flow__state {
            width: 16px;
            height: 16px;
            padding-top: 1px;
            border: 1px solid @color-primary;
            color: @color-primary;
            border-radius: 50%;
            background-color: @color-white;
        }
        .weui-input {
            height: 28px;
            line-height: 28px;
        }
        .weui-wepay-flow, .weui-wepay-flow-auto {
            padding: 10px 40px 30px;
            background: @color-white;
        }
        .weui-wepay-flow__li_done .weui-wepay-flow__state,
        .weui-wepay-flow__process {
            background-color: @color-primary;
        }
    }

    .vux-search-fixed {
        position: static !important;
    }

    .weui-search-bar__cancel-btn {
        color: #000000;
    }

    .list_mt_20 {
        .list_liebiao:first-child {
            margin-top: 0 !important;
        }
        .list_liebiao {
            margin-top: 1rem;
        }
    }

    .list_liebiao {
        margin-top: 1rem;
    }

    .list_liebiao {
        padding: 1rem 1rem 1rem 1rem;
        background: @color-white;
        h4 {
            font-size: @font-size-primary;
            font-weight: normal;
            padding-bottom: 0.3rem;
        }
        div span {
            display: inline-block;
            font-size: @font-size-medium;
            padding-bottom: 0.3rem;
        }
        div > span:last-child {
            padding-left: 2rem;
        }
        p {
            font-size: @font-size-medium;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
        }
    }

</style>
